/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;
import model.Contact;

/**
 * Form to add a contact
 *
 * @author gramage
 */
public class FormAdd extends JFrame implements ActionListener {

    /**
     * Arraylist of contact
     */
    protected ArrayList al;

    /**
     * Main container
     */
    protected JPanel cont;

    /**
     *
     */
    protected JLabel l_name;

    /**
     *
     */
    protected JLabel l_firstName;

    /**
     *
     */
    protected JLabel l_address;

    /**
     *
     */
    protected JLabel l_pPhone;

    /**
     *
     */
    protected JLabel l_proPhone;

    /**
     *
     */
    protected JLabel l_home;

    /**
     *
     */
    protected JLabel l_email;

    /**
     *
     */
    protected JLabel l_group;

    /**
     *
     */
    protected JTextField t_name;

    /**
     *
     */
    protected JTextField t_firstName;

    /**
     *
     */
    protected JTextField t_address;

    /**
     *
     */
    protected JTextField t_pPhone;

    /**
     *
     */
    protected JTextField t_proPhone;

    /**
     *
     */
    protected JTextField t_home;

    /**
     *
     */
    protected JTextField t_email;

    /**
     *
     */
    protected JComboBox cGroup;

    /**
     *
     */
    protected JButton button;

    protected JButton image;
    protected String imageFile;

    /**
     * Constructor
     *
     * @param ali Arraylist of contact
     */
    public FormAdd(ArrayList ali) {
        super("Add a contact");
        this.setSize(220, 440);
        this.setDefaultCloseOperation(FormAdd.DISPOSE_ON_CLOSE);
        al = ali;
        // Container
        cont = new JPanel();
        cont.setLayout(new BoxLayout(cont, BoxLayout.Y_AXIS));
        // Name
        l_name = new JLabel("Name :");
        cont.add(l_name);
        t_name = new JTextField(15);
        cont.add(t_name);

        // First name
        l_firstName = new JLabel("First Name :");
        cont.add(l_firstName);
        t_firstName = new JTextField(15);
        cont.add(t_firstName);

        // Address
        l_address = new JLabel("Address :");
        cont.add(l_address);
        t_address = new JTextField(15);
        cont.add(t_address);

        // Personnal phone
        l_pPhone = new JLabel("Phone :");
        cont.add(l_pPhone);
        t_pPhone = new JTextField(15);
        cont.add(t_pPhone);

        // Professional phone
        l_proPhone = new JLabel("Pro Phone :");
        cont.add(l_proPhone);
        t_proPhone = new JTextField(15);
        cont.add(t_proPhone);

        // Home
        l_home = new JLabel("Home :");
        cont.add(l_home);
        t_home = new JTextField(15);
        cont.add(t_home);

        // Mail
        l_email = new JLabel("Email :");
        cont.add(l_email);
        t_email = new JTextField(15);
        cont.add(t_email);

        // Group
        l_group = new JLabel("Group :");
        cont.add(l_group);
        String[] groupStrings = {"Family", "Friends", "Work", "Zoo"};
        cGroup = new JComboBox(groupStrings);
        cont.add(cGroup);

        //Add image
        image = new JButton("Add Image");
        imageFile = "";
        cont.add(image);
        image.addActionListener(this);

        //Button
        button = new JButton("Add");
        button.addActionListener(this);
        cont.add(button);

        // Add container
        cont.setVisible(true);
        this.add(cont);
        this.setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        //Save new contact
        if (e.getSource() == button) {
            al.add(new Contact(t_name.getText(), t_firstName.getText(), t_address.getText(), t_pPhone.getText(), t_proPhone.getText(), t_home.getText(), t_email.getText(), 0, imageFile));
            this.dispose();
        }
        if (e.getSource() == image) {
            JFileChooser chooser = new JFileChooser();
            chooser.showOpenDialog(null);
            imageFile= chooser.getSelectedFile().getAbsolutePath();
        }
    }

}
